package com.sanjiahantang.youjiarole.module.decorate.views.impl;

import android.content.Intent;
import android.graphics.Color;
import android.os.Bundle;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.RadioButton;
import android.widget.TextView;

import com.sanjiahantang.plugin.pickerview.OptionsPickerView;
import com.sanjiahantang.plugin.pickerview.TimePickerView;
import com.sanjiahantang.uilibrary.utils.Utils;
import com.sanjiahantang.youjiarole.R;
import com.sanjiahantang.youjiarole.base.BaseMvpActivity;
import com.sanjiahantang.youjiarole.base.BaseView;
import com.sanjiahantang.youjiarole.module.common.beans.ConditionBean;
import com.sanjiahantang.youjiarole.module.common.views.impl.SelectorActivity;
import com.sanjiahantang.youjiarole.module.decorate.beans.DecorateDicBean;
import com.sanjiahantang.youjiarole.module.decorate.presenters.DecorateManagePresenter;
import com.sanjiahantang.youjiarole.module.decorate.views.DecorateManageView;
import com.sanjiahantang.youjiarole.module.store.views.impl.StoreActivity;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.Date;


/**
 * Created by zy on 2017/8/10. 装修
 */

public class DecorateActivity extends BaseMvpActivity<DecorateManageView,DecorateManagePresenter> implements DecorateManageView, View.OnClickListener {

    private TextView title,txtJob,txtDoor,txtStyle,txtPrice,txtCreateTime,txtCity,txtPreTime;
    private EditText txtName,txtAddress,txtArea;
    private ArrayList<ConditionBean.SelectorData> jobList,doorList,styleList,priceList;
    private ConditionBean.SelectorData jobData,doorData,styleData,priceData;
    private OptionsPickerView pvOptions;
    private TimePickerView pvTime;

    private ArrayList<String> listProvince;
    private ArrayList<ArrayList<String>> listCity;
    private ArrayList<ArrayList<ArrayList<String>>> listArea;

    private RadioButton rb_man,rb_baby_yes,rb_old_yes;

    @Override
    public DecorateManagePresenter initPresenter() {
        return new DecorateManagePresenter();
    }

    @Override
    public void initView() {
        setContentView(R.layout.activity_decorate);
        title = (TextView) findViewById(R.id.title);
        title.setText("我要装修");
        findViewById(R.id.fl_Left).setOnClickListener(this);
        findViewById(R.id.ll_decorate_top).setVisibility(View.GONE);

        txtJob = (TextView) findViewById(R.id.txtJob);
        txtDoor = (TextView) findViewById(R.id.txtDoor);
        txtStyle = (TextView) findViewById(R.id.txtStyle);
        txtPrice = (TextView) findViewById(R.id.txtPrice);
        txtCreateTime = (TextView) findViewById(R.id.txtCreateTime);
        txtCity = (TextView) findViewById(R.id.txtCity);
        txtPreTime = (TextView) findViewById(R.id.txtPreTime);

        txtName = (EditText) findViewById(R.id.txtName);
        txtAddress = (EditText) findViewById(R.id.txtAddress);
        txtArea = (EditText) findViewById(R.id.txtArea);

        rb_man = (RadioButton) findViewById(R.id.rb_man);
        rb_baby_yes = (RadioButton) findViewById(R.id.rb_baby_yes);
        rb_old_yes = (RadioButton) findViewById(R.id.rb_old_yes);

        findViewById(R.id.vJob).setOnClickListener(this);
        findViewById(R.id.vDoor).setOnClickListener(this);
        findViewById(R.id.vStyle).setOnClickListener(this);
        findViewById(R.id.vPrice).setOnClickListener(this);
        findViewById(R.id.vCity).setOnClickListener(this);
        findViewById(R.id.vPreTime).setOnClickListener(this);
        findViewById(R.id.btnDill).setOnClickListener(this);
        TextView btnSubmit = (TextView) findViewById(R.id.btnDill);
        btnSubmit.setText("提交");
        findViewById(R.id.btn_decorate_nearshop).setVisibility(View.INVISIBLE);

        presenter.setOnLoadListener(new OnLoadListener() {
            @Override
            public void loadData(DecorateDicBean.Data data) {
                jobList = (ArrayList<ConditionBean.SelectorData>) data.getDutyList();
                doorList = (ArrayList<ConditionBean.SelectorData>) data.getDoorList();
                styleList = (ArrayList<ConditionBean.SelectorData>) data.getStyleList();
                priceList = (ArrayList<ConditionBean.SelectorData>) data.getBudgetList();
            }
        });

        init();
    }

    private void init() {
        Calendar c = Calendar.getInstance();
        txtCreateTime.setText(c.get(Calendar.YEAR) + " / " + (1 + c.get(Calendar.MONTH)) + " / " + c.get(Calendar.DAY_OF_MONTH) );

        presenter.loadCityList($this, new BaseView.OnLoadListener() {
            @Override
            public void onLoad(Object content) {
                Object[] listArray = (Object[]) content;
                listProvince = (ArrayList<String>) listArray[0];
                listCity = (ArrayList<ArrayList<String>>) listArray[1];
                listArea = (ArrayList<ArrayList<ArrayList<String>>>) listArray[2];
                pvOptions = new OptionsPickerView.Builder($this, new OptionsPickerView.OnOptionsSelectListener() {
                    @Override
                    public void onOptionsSelect(int options1, int options2, int options3, View v) {
                        //返回的分别是三个级别的选中位置
                        String tx = listProvince.get(options1) +  " " +
                                listCity.get(options1).get(options2) +  " " +
                                listArea.get(options1).get(options2).get(options3);
                        txtCity.setText(tx);
                        //    presenter.setData(context,txtUserName.getText().toString(),"",txtJob.getText().toString(),txtLocation.getText().toString(),txtInterest.getText().toString(),"address","");

                    }
                })

                        .setTitleText("")
                        .setDividerColor(Color.BLACK)
                        .setTextColorCenter(Color.BLACK) //设置选中项文字颜色
                        .setContentTextSize(20)
                        .build();

        /*pvOptions.setPicker(options1Items);//一级选择器
        pvOptions.setPicker(options1Items, options2Items);//二级选择器*/
                pvOptions.setPicker(listProvince, listCity,listArea);//三级选择器
            }
        });

        //控制时间范围(如果不设置范围，则使用默认时间1900-2100年，此段代码可注释)
        //因为系统Calendar的月份是从0-11的,所以如果是调用Calendar的set方法来设置时间,月份的范围也要是从0-11
        Calendar selectedDate = Calendar.getInstance();
        Calendar startDate = Calendar.getInstance();
        startDate.set(2017, 0, 23);
        Calendar endDate = Calendar.getInstance();
        endDate.set(2029, 11, 28);
        //时间选择器
        pvTime = new TimePickerView.Builder(this, new TimePickerView.OnTimeSelectListener() {
            @Override
            public void onTimeSelect(Date date, View v) {//选中事件回调
                // 这里回调过来的v,就是show()方法里面所添加的 View 参数，如果show的时候没有添加参数，v则为null
                /*btn_Time.setText(getTime(date));*/
                txtPreTime.setText(getTime(date));
            }
        })
                //年月日时分秒 的显示与否，不设置则默认全部显示
                .setType(new boolean[]{true, true, true, false, false, false})
                .setLabel("", "", "", "", "", "")
                .isCenterLabel(false)
                .setDividerColor(Color.DKGRAY)
                .setContentSize(21)
                .setDate(selectedDate)
                .setRangDate(startDate, endDate)
                .setBackgroundId(0x00FFFFFF) //设置外部遮罩颜色
                .setDecorView(null)
                .build();

    }

    private String getTime(Date date) {//可根据需要自行截取数据显示
        SimpleDateFormat format = new SimpleDateFormat("yyyy-MM-dd");
        return format.format(date);
    }

    @Override
    public void initData() {
        presenter.loadIndexDic($this);
    }

    @Override
    public void showProgress() {
        super.showProgress($this);
    }

    @Override
    public DecorateManagePresenter getPresenter() {
        return presenter;
    }

    @Override
    public void onClick(View view) {
        switch (view.getId()){
            case R.id.fl_Left:
                finish();
                break;
            case R.id.vJob:
                if (jobList != null){
                    SelectorActivity.startActivity($this,jobList,"选择职业",INTENT_JOB);
                }
                break;
            case R.id.vDoor:
                if (doorList != null){
                    SelectorActivity.startActivity($this,doorList,"选择户型",INTENT_DOOR);
                }
                break;
            case R.id.vStyle:
                if (styleList != null){
                    SelectorActivity.startActivity($this,styleList,"选择风格",INTENT_STYLE);
                }
                break;
            case R.id.vPrice:
                if (priceList != null){
                    SelectorActivity.startActivity($this,priceList,"选择预算",INTENT_PRICE);
                }
                break;
            case R.id.vCity:
                pvOptions.show();
                break;
            case R.id.vPreTime:
                pvTime.show();
                break;
            case R.id.btnDill:
                checkSubmit();
                break;
            case R.id.btn_decorate_nearshop:
                Intent intent = new Intent($this, StoreActivity.class);
                startActivity(intent);
                finish();
                break;
        }
    }

    private void checkSubmit() {
        if (TextUtils.isEmpty(txtName.getText())){
            Utils.showToast($this,"请填写姓名");
            return;
        }

        if (jobData == null){
            Utils.showToast($this,"请选择职业");
            return;
        }

        if (TextUtils.isEmpty(txtCity.getText())){
            Utils.showToast($this,"请选择所在地");
            return;
        }

        if (TextUtils.isEmpty(txtAddress.getText())){
            Utils.showToast($this,"请填写您的小区名称");
            return;
        }

        if (doorData == null){
            Utils.showToast($this,"请选择户型");
            return;
        }

        if (styleData == null){
            Utils.showToast($this,"请选择您喜欢的风格");
            return;
        }

        if (TextUtils.isEmpty(txtArea.getText())){
            Utils.showToast($this,"请填写面积");
            return;
        }

        if (priceData == null){
            Utils.showToast($this,"请选择您的预算范围");
            return;
        }

        if (TextUtils.isEmpty(txtPreTime.getText())){
            Utils.showToast($this,"请选择您的预计装修时间");
            return;
        }

        presenter.saveUserRequireInfoIntention(
                $this,
                txtName.getText().toString(),
                rb_man.isChecked() ? 0 : 1,
                jobData,
                txtCity.getText().toString(),
                txtAddress.getText().toString(),
                rb_baby_yes.isChecked() ? 1 : 0,
                rb_old_yes.isChecked() ? 1 : 0,
                doorData,
                styleData,
                txtArea.getText().toString(),
                priceData,
                txtPreTime.getText().toString()
        );
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (resultCode == RESULT_OK){
            switch (requestCode){
                case INTENT_JOB:
                    jobData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (jobData != null){
                        txtJob.setText(jobData.getDicName());
                    }
                    break;
                case INTENT_DOOR:
                    doorData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (doorData != null){
                        txtDoor.setText(doorData.getDicName());
                    }
                    break;
                case INTENT_STYLE:
                    styleData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (styleData != null){
                        txtStyle.setText(styleData.getDicName());
                    }
                    break;
                case INTENT_PRICE:
                    priceData = (ConditionBean.SelectorData) data.getSerializableExtra(KEY_PARAM);
                    if (priceData != null){
                        txtPrice.setText(priceData.getDicName());
                    }
                    break;
            }
        }
    }

}
